import BaseUtil from '@/BaseUtil';
import Buff from '@/Buff';
import BuffUtil from '@/BuffUtil';
import ObjectPool from '@/ObjectPool';
import SelectUtil from '@/SelectUtil';
import { OrderId } from '@/globals/order';
import BuffCommon from 'code/Core/Common/BuffCommon';
import UnitCommon from 'code/Core/Common/UnitCommon';
import AbilityBase from 'code/Scripts/Abilities/base/AbilityBase';
/**
 * 火焰雨
 **/
export default class SS_FireRain extends AbilityBase {
    static instance: SS_FireRain;
    buffType: AppBuffType = {
        id: 'buff_SS_FireRain',
        art: this.art,
        name: this.abilityName,
        dur: 3,
        interval: 0.5,
        ubertip: '该单位正在受到火雨的灼烧',
        onInterval: this.buffInterval.bind(this),
    };
    constructor(id: string = 'Ahyy') {
        super(id);
        SS_FireRain.instance = this;
        this.init();
        se.onUnitSpellEffect(this.onSpellEffect.bind(this), this.id);
    }
    init() {
        BuffUtil.registerBuffType(this.buffType);
    }
    cast<T extends Object = any>(tarX: number, tarY: number, source: unit, target: unit, data?: T): void {
        let vest = this.getVest(source, tarX, tarY);
        IssuePointOrderById(vest, OrderId.Rainoffire, tarX, tarY);
        BaseUtil.runLater(10, () => this.pool.returnObject(vest));
        BaseUtil.onTimer(1, (c) => {
            let lv = this.getAbilityLevel(source);
            let es = SelectUtil.getEnemyUnitsInRange(source, 250 + 50 * lv, tarX, tarY);
            for (let e of es) {
                // BuffUtil.addBuff(e, this.buffType.id, source);
                BuffCommon.addBuff({ target: e, caster: source, buffTypeId: this.buffType.id, ablityId: this.id });
            }
            let dmg = (25 + UnitCommon.取法强(source) * 0.35) * lv;
            DamageSystemInstance.applyAoeMagicDamage(es, source, dmg, 'fire', this.id);
            return c < 6;
        });
    }
    onSpellEffect() {
        this.cast(GetSpellTargetX(), GetSpellTargetY(), GetTriggerUnit(), GetSpellTargetUnit());
    }
    buffInterval(buff: Buff) {
        let lv = this.getAbilityLevel(buff.getCaster());
        let dmg = (20 + UnitCommon.取法强(buff.getCaster()) * 0.4) * lv * 0.1;
        DamageSystemInstance.applyFireDamage(buff.getParent(), buff.getCaster(), dmg, this.id, true);
    }
    pool: ObjectPool<unit> = new ObjectPool<unit>(() => {
        let vest = CreateUnit(Player(0), 'ewnv', 0, 0, 0);
        UnitAddAbility(vest, 'Ahym');
        return vest;
    });
    getVest(所有者: unit, tarX: number = 0, tarY: number = 0) {
        let v = this.pool.borrowObject();
        SetUnitLifePercentBJ(v, 100);
        SetUnitManaPercentBJ(v, 100);
        SetUnitOwner(v, GetOwningPlayer(所有者), false);
        SetUnitAbilityLevel(v, 'Ahym', GetUnitAbilityLevel(所有者, this.id));
        if (tarX && tarY) SetUnitPosition(v, tarX, tarY);
        return v;
    }
}
